﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace QLTV.HeThongXuLy
{
    class DocGiaDB
    {
        public static bool KiemMaLoaiDocGia(DocGia docgia)
        {
            string sql = "select * from LOAIDOCGIA where MaLoaiDocGia = '" + docgia.MaLoaiDocGia + "'";
            DataTable dt = ConnectDB.ExecQuery(sql);
            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static String TimTen(string Ma)
        {
            string sql = "select HoTenDocGia from DOCGIA where MaDocGia = '" + Ma + "'";
            DataTable dt = ConnectDB.ExecQuery(sql);
            if (dt.Rows.Count != 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "";
            }
        }

        public static DataTable LayDanhSachLoaiDocGia()
        {
            string sql = "Select * from LOAIDOCGIA";
            return ConnectDB.ExecQuery(sql);
        }
      
        public static bool TaoLoaiDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "INSERT INTO LOAIDOCGIA VALUES ('" + docgia.MaLoaiDocGia + "','"
                    + docgia.TenLoaiDocGia + "')";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message + ":Error");
                return false;

            }

        }
        public static bool SuaLoaiDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "UPDATE LOAIDOCGIA SET TenLoaiDocGia='" + docgia.TenLoaiDocGia +
                    "' where MaLoaiDocGia='" + docgia.MaLoaiDocGia + "'"; ;

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message + ":Error");
                return false;

            }

        }
        public static bool XoaLoaiDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "Delete from LOAIDOCGIA where MaLoaiDocGia='" + docgia.MaLoaiDocGia + "'";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message + ":Error");
                return false;

            }
        }


        ///////////////////////----------------Lap the doc gia -------////
        public static DataTable LayDanhSachDocGia()
        {
            string sql = "Select * from DOCGIA";
            return ConnectDB.ExecQuery(sql);
        }
        public static DataTable LayMaTuDong()
        {
            string sql = "Select MaDocGia from DOCGIA ORDER BY MaDocGia DESC";
            return ConnectDB.ExecQuery(sql);
        }
        public static bool ThemDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "INSERT INTO DOCGIA VALUES ('" + docgia.MaDocGia + "','"
                    + docgia.HoTen + "','"
                    + docgia.MaLoaiDocGia + "','"
                    + docgia.NgaySinh + "','"
                    + docgia.DiaChi + "','"
                    + docgia.DienThoai + "','"
                    + docgia.EMail + "','"
                    + docgia.NgayLapThe + "','"
                    + docgia.MaNhanVien + "','"                    
                    + docgia.NgayHetHan + "','"
                    + docgia.TienNo + "')";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message + ":Error");
                return false;

            }
        }
        public static bool SuaDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "UPDATE DOCGIA SET HoTenDocGia='" + docgia.HoTen +
                    "', NgaySinh='" + docgia.NgaySinh +
                    "', DiaChi='" + docgia.DiaChi +
                    "', DienThoai='" + docgia.DienThoai +
                    "', EMail='" + docgia.EMail +
                    "', NgayLapThe='" + docgia.NgayLapThe +
                    "', MaNhanVien='" + docgia.MaNhanVien +
                    "', MaLoaiDocGia='" + docgia.MaLoaiDocGia +
                    "', NgayHetHan='" + docgia.NgayHetHan +
                    "' where MaDocGia='" + docgia.MaDocGia + "'";
                

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message + ":Error");
                return false;

            }
        }
        public static bool XoaDocGia(DocGia docgia)
        {
            try
            {
                string sql =
                    "Delete from DOCGIA where MaDocGia='" + docgia.MaDocGia + "'";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message + ":Error");
                return false;

            }

        }
    }
}
